﻿using System;
using Abp.Dependency;
using Abp.Threading.Timers;
using System.Collections.Generic;
using Abp.Threading.BackgroundWorkers;

namespace ARchGL.Platform.BackgroundWorker
{
    /// <summary>
    /// 清理巡检/自检过期报表缓存
    /// </summary>
    public class CleanExpireCacheWorker : PeriodicBackgroundWorkerBase, ISingletonDependency
    {
        public static Dictionary<string, Dictionary<string, int[]>> ChecksOverviewCache = new Dictionary<string, Dictionary<string, int[]>>();

        public CleanExpireCacheWorker(AbpTimer timer) : base(timer)
        {
            //Timer.Period = 5000;
            Timer.Period = 600000; //10分钟一次轮询
        }
        
        protected override void DoWork()
        {
            var keys = new List<string>(ChecksOverviewCache.Keys);
            for (int i = 0; i < keys.Count; i++)
            {
                var key = keys[i];
                var date = DateTime.Parse(key.Substring(36, key.Length - 36));

                if (DateTime.UtcNow.Subtract(date).TotalMinutes < 10) continue;

                ChecksOverviewCache.Remove(key); //移除过期缓存
            }
        }
    }
}
